<template>
  <view class="main">
    <!-- 标题，logo -->
    <view class="top">
      <image class="image" src="/static/image/logo.png" />
      <text style="margin-left: 8px; font-weight: 500;">{{ title }}</text>
    </view>
    <!-- 内容 -->
    <text style="margin-top: 14px; font-size: 14px; flex: 1;">{{ content }}</text>
    <!-- 底部按钮 -->
    <view class="bottom">
      <view style="display: flex;">
          <button
          open-type="getPhoneNumber"
          size="mini"
          type="primary"
          :plain="true"
          style="display: flex;width: 240rpx; height: 80rpx;  justify-content: center; align-items: center;font-size: 34rpx;font-weight: 500;"
          @getphonenumber="getPhoneNumber"
        >
          同意授权
        </button>
        <button
          size="mini"
          plain="true"
          style="display: flex;width: 240rpx; height: 80rpx; margin-left: 32px; justify-content: center; align-items: center; border-color: white; background-color: #f5f5f5;font-size: 34rpx;font-weight: 500;"
          type="primary"
          @click="$u.throttle(close, 500)"
        >
          拒绝
        </button>

      </view>
    </view>
  </view>
</template>

<script>
import {
  AliByMobile,
  vxByMobile,
  vxMobile,
  wxJsCode
} from '@/api/login';
import { getLoginInfoFn } from '@/common/common'
export default {
  name: 'Index',
  props: {
    pageType: {
      type: String,
      default: ''
    },
    branchData: {
      type: Object,
      default: null
    },
    callbackUrl: {
      type: String,
      default: ''
    },
    urlParams: {
      type: String,
      default: ''
    },
  },
  data() {
    return {
      title: '鑫方盛 申请',
      content: '为了更好的用户体验，需要您授权手机号',
    }
  },
  methods: {
    close() {
        this.$emit('closePopFn', false)
    },
    getPhoneNumber(res) {
      console.log('同意授权code',res.detail.code,this.pageType)
      console.log(this.pageType)
      const iv = res.detail.iv
      const encryptedData = res.detail.encryptedData
      if (!encryptedData) {
        this.$u.toast('获取手机号码失败')
        this.close()
        return
      }
      let registParams = {}
      if (this.branchData && this.branchData && Object.keys(this.branchData).length > 0) {
        registParams = this.branchData
      } else {
        registParams = (this.registParams && Object.keys(this.registParams).length > 0) ? this.registParams : this.vuex_sharePushmanCompanyInfo
      }
      const { branchId = '', branchName = '', settleType } = registParams || {}
      // #ifdef MP-WEIXIN
      if(this.pageType =='question'){
        // const params ={
        // encryptedData: encryptedData,
        // iv: iv,
        // code:this.vuex_login_code
        // }
        //  vxMobile(params).then((result) => {
        //     this.$u.vuex('vuex_accountInfo', result.data)
        //     //this.$u.vuex('vuex_loginStatus', true)
        //      this.$u.vuex('vuex_questStatus', true)
        //     this.$emit('closePopFn','currentPage','agree',result.data.mobilePhone, false)
        //      return
        //  })
        const params ={
          branchId: branchId || null, //销售公司id
          branchName: branchName || null, //销售公司名称
          code:this.vuex_login_code
        }
        if (this.vuex_mapAddress) {
          // 小程序新增登录/注册所在地信息
          const { provinceName, cityName, districtName } = this.vuex_mapAddress
          params.commonParamProvinceName = provinceName
          params.commonParamCityName = cityName
          params.commonParamDistrictName = districtName
        }
        wxJsCode(params).then((res) => {
          this.$u.vuex('vuex_accountInfo', res.data) //res.data
          if (res.errorCode == 0) {
            this.$nextTick(() => {
              getLoginInfoFn(res.data.memberId)
              this.$u.vuex('vuex_loginStatus', true)
              this.vxMobileFn(encryptedData,iv,branchId,branchName,settleType,'question')
            })
          }
          if (res.errorCode == 2) {
            this.$u.vuex('vuex_accountInfo', res.data)
             this.vxMobileFn(encryptedData,iv,branchId,branchName,settleType,'question')
          }
          if (res.errorCode == 1) {
            this.$u.toast(res.msg)
          }
        }).catch(() => {
          // eslint-disable-next-line no-mixed-spaces-and-tabs
        })
      }else{
       this.vxMobileFn(encryptedData,iv,branchId,branchName,settleType,'')
      // const { unionid, openid } = this.vuex_accountInfo
      // const { nickName, avatar } = this.vuex_userInfo
      // const wxParams = {
      //   encryptedData: encryptedData,
      //   iv: iv,
      //   nickname: nickName,
      //   headimgurl: avatar,
      //   unionId: unionid,
      //   openId: openid,
      //   memberAscriptionId: branchId || null,
      //   memberAscriptionName: branchName || null,
      //   settleType: settleType || null,
      //   inviteCode: uni.getStorageSync('inviteCode'),
      //   appFlag: 0 // "小程序和M站区分[0-小程序 1-M站]
      // }
      // this.close()
      // vxByMobile(wxParams).then((result) => {
      //   if (result.errorCode != 0) {
      //     this.$u.toast('注册失败')
      //     return
      //   }
      //   this.$nextTick(() => {
      //     getLoginInfoFn(result.data.memberId)
      //     this.$u.vuex('vuex_accountInfo', result.data)
      //     this.$u.vuex('vuex_loginStatus', true)
      //     this.tryParams()
      //   })
      // })
    }
      // #endif
      // #ifdef MP-ALIPAY
      const aliParams = {
        encryptedData: encryptedData,
        userId: this.vuex_accountInfo.userId,
        memberAscriptionId: branchId || null,
        memberAscriptionName: branchName || null,
        inviteCode: uni.getStorageSync('inviteCode'),
        appFlag: 0 // "小程序和M站区分[0-小程序 1-M站]
        // inviteCode: 3287
      }
      this.close()
      AliByMobile(aliParams).then((result) => {
        if (result.errorCode != 0) {
          this.$u.toast('注册失败')
          return
        }
        this.$nextTick(() => {
          getLoginInfoFn(result.data.memberId)
          this.$u.vuex('vuex_accountInfo', result.data)
          this.$u.vuex('vuex_loginStatus', true)
          this.tryParams()
        })
      }).catch(() => {
      })
      // #endif
    },
    vxMobileFn(encryptedData,iv,branchId,branchName,settleType,type){
      const { unionid, openid } = this.vuex_accountInfo
      const { nickName, avatar } = this.vuex_userInfo
      const wxParams = {
        encryptedData: encryptedData,
        iv: iv,
        nickname: nickName,
        headimgurl: avatar,
        unionId: unionid,
        openId: openid,
        memberAscriptionId: branchId || null,
        memberAscriptionName: branchName || null,
        settleType: settleType || null,
        inviteCode: uni.getStorageSync('inviteCode'),
        appFlag: 0 // "小程序和M站区分[0-小程序 1-M站]
      }
      if(type ==''){
       this.close()
      }
      vxByMobile(wxParams).then((result) => {
        if (result.errorCode != 0) {
          this.$u.toast('注册失败')
          return
        }
        this.$nextTick(() => {
          getLoginInfoFn(result.data.memberId)
           if(type =='question'){
              this.$emit('closePopFn','currentPage','agree',result.data.mobilePhone, false)
           }
          this.$u.vuex('vuex_accountInfo', result.data)
          this.$u.vuex('vuex_loginStatus', true)
          this.tryParams()
          
        })
      })
    },
    tryParams() {
      if (this.pageType == 'pay') {
        this.$emit('paymentFn')
      } else if (this.pageType == 'order') {
        uni.redirectTo({ url: this.callbackUrl })
      } else if (this.pageType == 'my') {
        uni.redirectTo({ url: this.callbackUrl })
      } else if (this.pageType == 'ground') {
        uni.redirectTo({ url: this.callbackUrl })
      } else {
        let paramStr = ''
        const urlParams = this.urlParams ? JSON.parse(this.urlParams) : {}
        for (const urlParamsKey in urlParams) {
          paramStr += urlParamsKey + '=' + urlParams[urlParamsKey] + '&'
        }
        uni.redirectTo({ url: this.callbackUrl + '?' + paramStr })
      }
    }
  }
}
</script>

<style scoped>
.main{
  display: flex;
  flex-direction: column;
  border-radius: 24rpx 24rpx 0 0;
  background-color: white;
  padding-left: 48rpx;
  padding-right: 48rpx;
  height: 320rpx;
}

.top{
  display: flex;
  margin-top: 48rpx;
  color: black;
  font-size: 30rpx;
  justify-content: flex-start;
  align-items: center;
}

.image{
  width: 48rpx;
  height: 48rpx;
}

.bottom{
  display: flex;
  flex-direction: row;
  margin-bottom: 64rpx;
  justify-content: center;
  align-items: center;
}
</style>
